package com.wsc.wisdomchina.activitity.wisdomParty;

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.wsc.wisdomchina.R;
import com.wsc.wisdomchina.Tools.JsonTools;
import com.wsc.wisdomchina.Tools.Tools;
import com.wsc.wisdomchina.activitity.SMSActivity;
import com.wsc.wisdomchina.activitity.baseactivity.BaseActivity;
import com.wsc.wisdomchina.adapter.FloorSearchAdapter;
import com.wsc.wisdomchina.callback.ContributorView;
import com.wsc.wisdomchina.constant.Constant;
import com.wsc.wisdomchina.javaBean.FloorInfoSearchBean;
import com.wsc.wisdomchina.javaBean.RespObj;
import com.wsc.wisdomchina.javaBean.wisdomParty.PartyUserBean;
import com.wsc.wisdomchina.presenter.ContributorPresenter;
import com.wsc.wisdomchina.view.MyToast;


public class PartyLoginActivity extends BaseActivity implements View.OnClickListener,ContributorView{
    private ImageView iv_close;
    private TextView tv_title;
    private Button bt_finish;
    private EditText et_number;
    private EditText sms_code;
    private TextView tv_smscode;
    private MyToast myToast;
    private Context context;
    private ContributorPresenter contributorPresenter;
    private final String PMIVERFIFICATION="PMIVERFIFICATION";
    private String cid;
    private boolean isUserExit;
    private TimeCount registerCheck;
    private final String USER_SEND_VCODE="USER_SEND_VCODE";
    private final String USER_JUDGE_VCODE="USER_JUDGE_VCODE";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.partylogin_layout);
        initView();
        Intent intent=getIntent();
        cid=intent.getStringExtra("cid");
    }

    private void initView()
    {
        context=this;
        registerCheck = new TimeCount(60000, 1000);
        contributorPresenter=new ContributorPresenter(this,context);
        myToast=new MyToast(context);
        tv_title=(TextView) findViewById(R.id.tv_title);
        Typeface typeFace = Typeface.createFromAsset(getAssets(),"fonts/lishu.ttf");
        // 应用字体
        tv_title.setTypeface(typeFace);
        iv_close=(ImageView) findViewById(R.id.iv_close);
        iv_close.setOnClickListener(this);
        bt_finish=(Button) findViewById(R.id.bt_finish);
        et_number=(EditText) findViewById(R.id.et_number);
        sms_code=(EditText) findViewById(R.id.sms_code);
        tv_smscode=(TextView) findViewById(R.id.tv_smscode);
        bt_finish.setOnClickListener(this);
        tv_smscode.setOnClickListener(this);
        et_number.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String phone = et_number.getText().toString().trim();
                if(phone.length()==11)
                {
                    if(Tools.isMobile(phone))
                    {
                        contributorPresenter.postPmiVerification(cid,phone,PMIVERFIFICATION);
                    }else
                    {
                        myToast.showShort("手机号码格式不对,请重试");
                    }
                }else if(phone.length()>11)
                {
                    myToast.showShort("手机号码格式不对,请重试");
                }
            }
        });
    }

    @Override
    public void onLoadContributorStart(String flag) {

    }


    @Override
    public void onLoadContributorSuccess(String jsonString, String flag) {
        try {
            //判断用户是否存在
            if (flag.equals(PMIVERFIFICATION)) {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code = respObj.getCode();
                Object object = respObj.getMessage();
                if (code.equals("200") && object != null) {
                    isUserExit=true;
                }else
                {
                    isUserExit=false;
                    myToast.showShort("用户信息不存在,请联系管理员");
                }
            }
            //发送验证码
            else if (flag.equals(USER_SEND_VCODE))
            {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code=respObj.getCode();
                if (code.equals("200")) {
                    myToast.showShort("发送成功,请稍后...");
                }else if(code.equals("10102"))
                {
                    myToast.showShort("手机号码不能为空,请核对手机号");
                }else if(code.equals("10600"))
                {
                    myToast.showShort("发送失败,请重试");
                }
            }
            //验证验证码
            else if(flag.equals(USER_JUDGE_VCODE))
            {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code=respObj.getCode();
                Object message=respObj.getMessage();
                if(code.equals("200")&& message!=null)
                {
                    String messageString=JsonTools.objectToJsonString(message);
                    PartyUserBean partyUserBean=JsonTools.jsonStringToObject(messageString,PartyUserBean.class);
                    Tools.setSharedPreferencesStrings(Constant.SharePreferencePartyUserInfo,messageString,context);
                    Intent intent=new Intent(context,PartyActivity.class);
                    intent.putExtra("pId",partyUserBean.getId());
                    intent.putExtra("cId",cid);
                    startActivity(intent);
                    finish();
                }
            }
        } catch (Exception e) {

        }
    }


    @Override
    public void onLoadContributorComplete(String flag) {
    }

    @Override
    public void onLoadContributorError(String error, String flag) {
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.iv_close:
                finish();
                break;
            //发送验证码
            case R.id.tv_smscode:
                if(isUserExit)
                {
                    String phone = et_number.getText().toString().trim();
                    btSendCode(phone);
                }else {
                    myToast.showShort("用户信息不存在");
                }
                break;
            //完成
            case R.id.bt_finish:
                String phone=et_number.getText().toString().trim();
                String vcode=sms_code.getText().toString().trim();
                if(phone.length()>0 && vcode.length()>0)
                {
                    if(isUserExit)
                    {
                        contributorPresenter.getVerificationComplete(phone,vcode,cid,USER_JUDGE_VCODE);
                    }else {
                        myToast.showShort("用户信息不存在");
                    }
                }else {
                    myToast.showShort("手机号码或者验证码不能为空");
                }
                break;
        }
    }

    //发送验证码
    private void btSendCode(String number) {
        registerCheck.start();
        contributorPresenter.postSendCode(number,USER_SEND_VCODE);
    }

    /* 定义一个倒计时的内部类 */
    class TimeCount extends CountDownTimer {

        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
        }

        @Override
        public void onFinish() {// 计时完毕时触发
            tv_smscode.setText("获取验证码");
            setSendRegisterCodeEnable();
        }

        @Override
        public void onTick(long millisUntilFinished) {// 计时过程显示
            tv_smscode.setText(millisUntilFinished / 1000 + "秒后重发");
            setSendRegisterCodeUnenable();
        }
    }
    private void setSendRegisterCodeEnable() {
        tv_smscode.setClickable(true);
        tv_smscode.setTextColor(getResources().getColor(R.color.OK_color));
    }
    private void setSendRegisterCodeUnenable() {
        tv_smscode.setClickable(false);
        tv_smscode.setTextColor(getResources().getColor(R.color.B2B2B2));
    }
}
